import React, { PureComponent } from 'react';
import { PageHeaderWrapper } from '@ant-design/pro-layout';
import { message, Card, Button, Modal } from 'antd';
import { StandardTable, SearchForm } from '@joy/joy-pro';
import { getValue, to } from '@/utils/smallTool';
import { connect } from 'dva';
import configData from './config-data';
import styles from './index.less';
import { updateBugState } from '@/services/statistics';

const pageInfo = { showSizeChanger: true, showQuickJumper: true, defaultPageSize: 20 };
const drawerWidth = 500;
const formProp = {
  labelCol: { span: 6 },
  wrapperCol: { span: 18 },
};
@connect(({ global, loading, bindfail, allcity, authority }) => ({
  tableData: bindfail.tableData,
  IssueChannelList: bindfail.IssueChannelList,
  SalespersonList: bindfail.SalespersonList,
  ManufactureList: bindfail.ManufactureList,
  cityList: allcity.cityList,
  searchForm: bindfail.searchForm,
  permissionsList: authority.permissionsList,
  tableLoading: loading.effects['bindfail/getBindFailList'],
  watermarkConfig: global.watermarkConfig,
}))
class BindFail extends PureComponent {
  constructor(props) {
    super(props);
    this.$drawerForm = React.createRef();
    this.state = {
      selectedRows: [],
      shieHideBatchDel: false,
      params: {},
      pageRequest: {
        pageNum: 1,
        pageSize: 10,
      },
      drawerStatus: {
        visible: false,
        type: 'add',
      },
      resultDate: [],
      loading: true,
      activeRecord: {},
      inputConfig: configData.getSearchInputConfig(props.cityList)
    };
    this.getSelectList()
    this.refreshList();
  }
  refreshList() {
    const { params } = this.state;
    this.getQueryList(params, { pageNum: 1, pageSize: 10 })
  };
  /**
   * @description 条件筛选 [搜索]
   * @author zhe
   * @date 2020-04-13
   * @param {*} formData
   * @param {*} reaclData
   * @param {*} isClear
   * @memberof
   */
  actionSearch = (formData, reaclData, isClear) => {
    const params = formData;
    const pageRequest = { pageNum: 1, pageSize: 10 };
    this.setState({
      params: params,
    });
    this.getQueryList(params, pageRequest);
  };

  /**
   * @description 请求列表数据
   * @author zhe
   * @date 2020-04-13
   * @param {*} params
   * @param {*} pageRequest
   * @memberof
   */
  getQueryList = (params, pageRequest) => {
    const type = 'bindfail/getBindFailList';
    const param = Object.assign(pageRequest, params);
    const { dispatch } = this.props;
    dispatch({
      type,
      payload: param,
    });
  };
  /**
   * @description 请求事件类型下拉列表数据
   * @author zhe
   * @date 2020-04-14
   * @memberof
   */
  getSelectList = async () => {
    const type = 'allcity/getCityByPage';
    const { dispatch } = this.props;
    const cityList = await dispatch({
      type
    });
    this.setState({
      inputConfig: configData.getSearchInputConfig(cityList)
    })
  };
  /**
   * @description table操作触发改变事件
   * @author songs
   * @date 2019-07-12
   * @param {*} pagination
   * @param {*} filtersArg
   * @param {*} sorter
   * @memberof managementUser
   */
  handleStandardTableChange = (pagination, filtersArg, sorter, actionType) => {
    const { pageSize, current } = pagination;
    const pageNum = current;
    const pageRequest = { pageSize, pageNum };
    this.getQueryList(filtersArg, pageRequest);
  };
  async updateBugState(params) {
    try {
      const responseData = await updateBugState(params)
      if (responseData && responseData.code == '000000') {
        this.refreshList();
      }
    } catch (e) { }
  };
  handleChangeStatus(record, status) {
    Modal.confirm({
      content: '确定要修改此状态吗？',
      onOk: () => {
        this.updateBugState({ id: record.id, state: status });
      }
    })
  };
  tableCol = () => {
    const OptionNode = ({ record }) => (
      <div className={styles.tableButtonGroup}>
        <Button
          type="link"
          style={{ marginRight: '20px' }}
          disabled={record.status == 1}
          onClick={() => {
            this.handleChangeStatus(record, '1');
          }}
        >
          可用
        </Button>
        <Button
          type="link"
          style={{ marginRight: '20px' }}
          disabled={record.status == 2}
          onClick={() => {
            this.handleChangeStatus(record, '2');
          }}
        >
          不可用
        </Button>
      </div>
    );
    return configData.getTableConfig(OptionNode)
  };

  render() {
    const { tableCol } = this;
    const { resultDate, inputConfig } = this.state;
    const { tableLoading, watermarkConfig, tableData } = this.props;
    const { list = [], pagination = {} } = tableData;
    return (
      <div>
        <PageHeaderWrapper title="" className="hasContent">
          <div style={{ background: '#fff', padding: '20px 0px 0px 24px', marginBottom: '10px' }}>
            <SearchForm
              defaultInput={inputConfig.slice(0, 3)}
              moreInput={inputConfig.slice(3)}
              gutter={{ md: 10 }}
              useCol={{ sm: 24, md: 12, xl: 8, xxl: 8 }}
              onSearch={this.actionSearch}
            ></SearchForm>
          </div>
          <Card bordered={false}>
            <StandardTable
              isWatermark
              watermark={{ ...watermarkConfig }}
              dataSource={list}
              pagination={{ ...pagination, ...pageInfo }}
              columns={tableCol()}
              loading={tableLoading}
              rowKey={(record, index) => index}
              onChange={($pagination, filtersArg, sorter) => {
                this.handleStandardTableChange($pagination, this.state.params, sorter, 'bindfail/getBindFailList');
              }}
            ></StandardTable>
          </Card>
        </PageHeaderWrapper>
      </div>
    );
  }
}

export default BindFail;
